package com.learn.service;

import com.learn.mapper.BlogMapper;
import com.learn.pojo.Blog;
import com.learn.pojo.User;
import com.learn.pojo.vo.BlogAndUser;
import com.learn.pojo.vo.BlogVo;
import com.learn.util.QueryResult;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @description: 描述
 * @author: Mr.Hu
 * @create: 2022/3/20 18:24
 **/
@Service
public class BlogService {
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    UserService userService;

    public QueryResult<BlogVo> queryAll() {
        QueryResult<BlogVo> queryResult = new QueryResult<BlogVo>();
        List<Blog> blogs = blogMapper.queryAll();
        List<BlogVo> blogVos = new ArrayList<>();
        for (Blog blog : blogs) {
            System.out.println(blog.toString());
            BlogVo blogVo = new BlogVo();
            String s = userService.queryUserNameById(blog.getUserId());
            BeanUtils.copyProperties(blog, blogVo);
            blogVo.setUserName(s);
            blogVos.add(blogVo);
        }
//        Logger logger =new Logger()
        queryResult.setRows(blogVos);
        return queryResult;
    }

    public BlogVo queryDetailByBlogId(int blogId) {
        Blog blog = blogMapper.queryByBlogId(blogId);
        int userId = blog.getUserId();
        String s = userService.queryUserNameById(userId);
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(blog, blogVo);
        blogVo.setUserName(s);
        return blogVo;
    }

    public Integer addBlog(Blog blog) {
        return blogMapper.addBlog(blog);
    }
    public BlogVo queryDetailByBlogTitle(String blogTitle) {
        Blog blog = blogMapper.queryByBlogTitle(blogTitle);
        int userId = blog.getUserId();
        String s = userService.queryUserNameById(userId);
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(blog, blogVo);
        blogVo.setUserName(s);
        return blogVo;
    }
}
